/**
 * axios请求封装
 */
import axios from 'axios'
import { ElMessage } from 'element-plus'

const service = axios.create()

// service.defaults.baseURL = '/backend-api'
service.defaults.timeout = 50000 //  超时时间

//  请求拦截器
service.interceptors.request.use(
  (config) => {
    //  添加token
    if (localStorage.getItem('token')) {
      config.headers.Authorization = 'Bearer ' + localStorage.getItem('token')
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

//  响应拦截器
service.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    ElMessage.error(error.response.data.msg)
    return Promise.reject(error.response.data)
  }
)
export default service
